package com.xtaller.lazy.util;


import com.alibaba.fastjson.JSONObject;
import com.xtaller.lazy.lib.convert.J;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.SortingParams;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @version V1.0
 * @author: Taller
 * @date: 2019-04-19
 * @Description: redis操作类
 */
@Component
public class CacheUtil {
    @Autowired private StringRedisTemplate stringRedisTemplate;
    @Autowired private RedisTemplate redisTemplate;

    /**
     * 字符串设置
     * */
    public Boolean set(String key, Object val, int times) {
        try {
            if(val.getClass().isPrimitive()){
                stringRedisTemplate.opsForValue().set(key, val.toString(), times, TimeUnit.SECONDS);
            }else {
                stringRedisTemplate.opsForValue().set(key, J.o2s(val), times, TimeUnit.SECONDS);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    public Boolean set(String key, Object val) {
        try {
            if(val.getClass().isPrimitive()) {
                stringRedisTemplate.opsForValue().set(key, val.toString());
            }else {
                stringRedisTemplate.opsForValue().set(key, J.o2s(val));
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 字符串读取
     * */
    public String getVal(String key) {
        String val = "";
        try {
            val = stringRedisTemplate.opsForValue().get(key);
            val = val == null ? "" : val;
        } catch (Exception ex) {
        }
        return val;
    }



    /**
     * 字符串删除
     * */
    public void deleteVal(String key) {
        stringRedisTemplate.delete(key);
    }

    /**
     * 对象设置
     * */
    public void setObject(String key, Object object, int times) {
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
        if (times > 0){
            operations.set(key, object, times, TimeUnit.SECONDS);
        }
        operations.set(key, object);
    }
    public void setObject(String key, Object object) {
        setObject(key, object, 0);
    }
    /**
     * 对象读取
     * */
    public JSONObject getObject(String key) {
        JSONObject object = null;
        try {
            object = J.s2j(getVal(key));
        } catch (Exception e) {
        }
        return object;
    }
    public Map getMap(String key){
        try {
            return J.s2m(getVal(key));
        } catch (Exception e) {
            return null;
        }

    }
    public List<JSONObject> getList(String key) {
        List<JSONObject> object = null;
        try {
            object = J.s2l(getVal(key));
        } catch (Exception e) {
        }
        return object;
    }



    /**
     * 删除对象
     * */
    public void deleteObject(String key) {
        redisTemplate.delete(key);
    }

}
